import App from './App' 
import { serverUrl, ftpUrl, apiBase, storageTokenKey, contactPhone} from './global/config.js'
import { initInterceptors } from "./global/interceptors.js"
import { RequestCenter } from './http/request_center.js'
import share from './global/share.js'
import commonBehavior from './behavior/common-behavior.js'
import { resource } from '@/resource/resource.js'
import { Throwable } from '@/err_handle/throwable.js'
import { errTips } from "@/http/http_interceptor.js"

import CustomNavBar from './components/custom-nav-bar/custom-nav-bar.vue'
import CustomPage from './components/custom-page/custom-page.vue'
import MessageView from '@/components/message-view/message-view.vue'
import ListView from '@/components/common-list-view/common-list-view.vue'
import TopTabBar from '@/components/top-tab-bar/top-tab-bar.vue'
import { callWithAsyncErrorHandling } from "vue"
import * as filters from '@/common/commonFilter.js'

const isFunction = (val) => typeof val === "function";

const containerStyle = "max-width: 100%; margin: 0; overflow:hidden; list-style: none; word-wrap: break-word; background: white;"
const tagStyle = {
	"img": "width: 100% !important; display: block;"
}

const ASSERTS = new Proxy(resource, {
	get: function(target, key) {
		return ftpUrl + "/" + target[key]
	}
})

initInterceptors()

import { errHandle } from '@/err_handle/err_handle.js'
import { isPromise } from '@/utils/utils.js'

const requestCenter = new RequestCenter()
const requestCenterProxy = new Proxy({}, {
	get(target, key) {
		const wrapper = function() {
			if(requestCenter) {
				if(isFunction(requestCenter[key])) {
					return function() {
						let result = requestCenter[key].call(requestCenter, ...arguments)
						if(isPromise(result)) {
							result = result.catch((error) => {
								if(errHandle) {
									errHandle(error)
								}
								return Promise.reject()
							})
						}
						return result
					}
				} else {
					return requestCenter[key]
				}
			} else {
				return undefined
			}
		}
		return wrapper()
	}
})

// #ifndef VUE3
import Vue from 'vue'
Vue.config.productionTip = false
App.mpType = 'app'

Vue.prototype.serverUrl = serverUrl
Vue.prototype.storageTokenKey = storageTokenKey
Vue.prototype.ftpUrl = ftpUrl
Vue.prototype.apiBase = apiBase
Vue.prototype.contactPhone = contactPhone
// Vue.prototype.requestCenter = /*requestCenterProxy*/requestCenter
Vue.prototype.requestCenter = requestCenterProxy
Vue.prototype.containerStyle = containerStyle
Vue.prototype.tagStyle = tagStyle
Vue.prototype.isLoading = false
Vue.prototype.asserts = ASSERTS

Vue.component('custom-nav-bar', CustomNavBar)
Vue.component('custom-page', CustomPage)
Vue.component('message-view', MessageView)
Vue.component('common-list-view', ListView)
Vue.component('top-tab-bar', TopTabBar)

Vue.mixin(share)
Vue.mixin(commonBehavior)

const app = new Vue({
    ...App
})
app.$mount()
// #endif

// #ifdef VUE3
import { createSSRApp } from 'vue'
export function createApp() {
  const app = createSSRApp(App)
  
  app.config.globalProperties.serverUrl = serverUrl
  app.config.globalProperties.storageTokenKey = storageTokenKey
  app.config.globalProperties.ftpUrl = ftpUrl
  app.config.globalProperties.apiBase = apiBase
  app.config.globalProperties.contactPhone = contactPhone
  // app.config.globalProperties.requestCenter = /*requestCenterProxy*/requestCenter
  app.config.globalProperties.requestCenter = requestCenterProxy
  app.config.globalProperties.containerStyle = containerStyle
  app.config.globalProperties.tagStyle = tagStyle
  app.config.globalProperties.isLoading = false
  app.config.globalProperties.asserts = ASSERTS
  
  Object.keys(filters).forEach(key => {
  	app.config.globalProperties[key] = filters[key]
  })
  
  app.component('custom-nav-bar', CustomNavBar)
  app.component('custom-page', CustomPage)
  app.component('message-view', MessageView)
  app.component('common-list-view', ListView)
  app.component('top-tab-bar', TopTabBar)

  app.mixin(share)
  app.mixin(commonBehavior)
  
  return {
    app
  }
}
// #endif